<?php

namespace App\Admin\Controllers;

use App\model\Project;
use App\User;
use App\Http\Controllers\Controller;
use App\UserInfo;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Table;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {

        return $content
            ->header('业务员')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑业务员')
            ->description('')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建业务员')
            ->description('')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);


        $grid->id('Id');
        $grid->name('姓名')->editable();
        $grid->avatar('头像')->display(function($value){
            if ($value==''){
                $userInfo=UserInfo::whereUserId($this->id)->first();
                if ($userInfo){
                    $avatar= $userInfo->avatar;
                }else{
                    $avatar='';
                }
            }else{
               $avatar= \Storage::url($value);
            }
            return "<img src='$avatar' width='51px' height='50px'>";
        });

        $grid->role('角色')->display(function($value ){
            $user=User::find($this->id);
            $roles=$user->getRoleNames();
            $name='';
            if ($roles->isEmpty()){
                return '未设置';
            }
            foreach ($roles as $v){
                $name.="<span class='label label-success'>$v</span>&nbsp";
            }
            return $name;
        });
        $grid->project('所属项目')->expand(function ($model) {
            $comments = Project::whereIn('project_id',$model->project_id)->get()->map(function ($comment) {
                return $comment->only(['name']);
            });

            return new Table(['所属项目'], $comments->toArray());
        });

        $grid->no_project_id('禁止项目通知')->display(function($value ){

            $roles=Project::whereIn('project_id',$value)->get();
            $name='';
            if ($roles->isEmpty()){
                return "<span class='label label-success' style='margin-left:10px '>未设置</span>";
            }
            foreach ($roles as $k=>$v){
                $name.="<span class='label label-success' style='margin-left:10px;line-height: 40px '>$v->name</span>" ;
                if ($k%2==1){
                    $name.="</br>";
                }
            }
            return $name;
        });
        $grid->mobile('手机号')->editable();
        $grid->address('公司')->editable();
// 设置text、color、和存储值
        $states = [
            'off'  => ['value' => 0, 'text' => '未审核', 'color' => 'primary'],
            'on' => ['value' => 1, 'text' => '已审核', 'color' => 'default'],
        ];
        $grid->is_passed('是否通过审核')->switch($states);
     //   $grid->disableTools();
        $grid->actions(function(Grid\Displayers\Actions $actions){
            $actions->disableView();
        });
        $grid->disableExport();

      //  $grid->disableActions();
        $grid->created_at('创建时间');
        $grid->filter(function(Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->like('name','昵称');
            $filter->equal('mobile','手机号');
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('姓名');

        $show->password('Password');
        $show->remember_token('Remember token');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('name', '姓名');
        //$form->email('email', 'Email');
        $form->text('mobile','手机号');
        $form->text('address','公司');
        $form->image('avatar','头像');
        $form->hidden('password', 'Password')->default(bcrypt('123456'));
        $form->multipleSelect('project_id','项目')->options(Project::getSelectOptions());
        $form->multipleSelect('no_project_id','禁止项目通知')->options(Project::getSelectOptions());

        $states = [
            'off'  => ['value' => 0, 'text' => '未审核', 'color' => 'primary'],
            'on' => ['value' => 1, 'text' => '已审核', 'color' => 'default'],
        ];
        $form->switch('is_passed','是否通过审核')->states($states);
        $form->multipleSelect('roles','角色')->options(Role::all()->pluck('name', 'id'));
        $form->multipleSelect('permissions','额外权限')->options(Permission::all()->pluck('cn_name', 'id'))->help('可以单独设定用户设定的权限');
        return $form;
    }
}
